﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenPowerPoint.Exceptions;

namespace OpenPowerPointTest.Operations
{
    [TestFixture]
    [Category("Integration: Slide Operations")]
    public class Operations
    {
        #region Setup/Teardown
        [SetUp]
        public void Setup()
        {
            _presentation = new OpenPowerPoint.Presentation(OpenPowerPoint.Presentation.PresentationType.Presentation);
        }

        [TearDown]
        public void Teardown()
        {
            _presentation = null;
        }
        #endregion
        private OpenPowerPoint.Presentation _presentation;


        [Test]
        public void Slides()
        {
            var slide = _presentation.AddSlide();
            Assert.IsNotNull(slide, "addslide returned null");
            Assert.AreEqual(1, _presentation.SlideCount(), "slide not added");
            var slide2 = _presentation.GetSlide(0);
            Assert.AreSame(slide, slide2, "Slides' Get Accessor is not working");
            slide2 = _presentation.GetGroup(0).GetSlide(0);
            Assert.AreSame(slide, slide2, "Slide was in wrong group");

            //second test run
            var slide1 = _presentation.AddSlide(0);
            slide2 = _presentation.GetSlide(0);

            Assert.AreSame(slide1, slide2, "Slide not inserted at correct position");
            
            _presentation.RemoveSlide(slide);
            Assert.Throws<ArgumentOutOfRangeException>(() => _presentation.GetSlide(1), "Slide not removed");

            _presentation.RemoveSlide(0);
            Assert.Throws<ArgumentOutOfRangeException>(() => _presentation.GetSlide(0), "Slide at position 0 was not removed");
        }

        [Test]
        public void Groups()
        {
            Assert.AreEqual(1, _presentation.GroupCount(), "Initial Group missing");

            var group = _presentation.AddGroup("new group");
            Assert.IsNotNull(group, "addgroup returned null");

            var group2 = _presentation.GetGroup("new group");
            Assert.AreSame(group, group2, "Groups Get accessor is not working");

            var slide = group.AddSlide();

            _presentation.RemoveGroup("new group");

            Assert.Throws<ArgumentOutOfRangeException>(() => _presentation.GetGroup("new group"), "Group not removed");

            Assert.AreEqual(slide, _presentation.GetGroup(0).GetSlide(0), "Slide not moved to previous group at IsRemove");

            //remove slide again
            _presentation.RemoveSlide(0);

            //Next test run for groups
            group = _presentation.AddGroup("new group", 0);
            Assert.IsNotNull(group, "addgroup returned null");
            group2 = _presentation.GetGroup(0);
            Assert.AreSame(group, group2, "Group was not inserted at position 0");

            _presentation.RemoveGroup(0, false);
            group2 = _presentation.GetGroup(0);

            Assert.AreNotSame(group, group2, "Item at position 0 was not removed");
        }

        [Test]
        public void Masters()
        {
            Assert.AreEqual(1, _presentation.SlideMasterCount(), "No Initial SlideMaster");

            var master = _presentation.AddSlideMaster();
            Assert.IsNotNull(master, "addslidemaster returned null");
            var master2 = _presentation.GetSlideMaster(1);
            Assert.AreSame(master2, master, "SlideMaster's Get accessor isn't working");

            _presentation.RemoveSlideMaster(1);

            Assert.Throws<ArgumentOutOfRangeException>(() => _presentation.GetSlideMaster(1), "SlideMaster not removed");

            Assert.Throws<ListNotClearable>(() => _presentation.RemoveSlideMaster(0), "Last slide master was removed");
        }

        [Test]
        public void Layouts()
        {
            Assert.AreEqual(1, _presentation.GetSlideMaster(0).LayoutCount(), "No initial SlideLayout");

            var layout = _presentation.GetSlideMaster(0).AddLayout();
            Assert.IsNotNull(layout, "addlayout returned null");
            var layout2 = _presentation.GetSlideMaster(0).GetLayout(1);
            Assert.AreSame(layout, layout2, "SlideLayout's Get accessor isn't working");

            _presentation.GetSlideMaster(0).RemoveLayout(1);

            Assert.Throws<ArgumentOutOfRangeException>(() => _presentation.GetSlideMaster(0).GetLayout(1), "SlideLayout not removed");

            Assert.Throws<ListNotClearable>(() => _presentation.GetSlideMaster(0).RemoveLayout(0));
        }
    }
}
